package com.blog.controller.admin;

import com.blog.dto.AddCategoryByWorksDTO;
import com.blog.dto.WorksDTO;
import com.blog.result.Result;
import com.blog.service.WorksService;
import com.blog.vo.CategoryVO;
import com.blog.vo.WorksVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminWorksController")
@RequestMapping("/admin/Works")
@Slf4j
public class WorksController {

    @Autowired
    private WorksService worksService;
    @GetMapping("/list")
    public Result<List<WorksVO>> WorksList(){
        log.info("查询作品列表....");
        /**
         * 获取时间戳:
         * Date date = new Date();
         * Timestamp ts = new Timestamp(date.getTime());
         * */
        List<WorksVO> list = worksService.getWorksList();
        return Result.success(list);
    }


    @GetMapping("/worksById/{wid}")
    public Result<WorksVO> worksById(@PathVariable("wid") Long wid){
        log.info("根据作品ID查询作品：{}",wid);
        /**
         * 获取时间戳:
         * Date date = new Date();
         * Timestamp ts = new Timestamp(date.getTime());
         * */
        WorksVO worksVO = worksService.getOneWorks(wid);
        return Result.success(worksVO);
    }

    @GetMapping("/worksByName/{value}")
    public Result<List<WorksVO>> worksByName(@PathVariable("value") String value){
        log.info("根据作品名称模糊查询作品：{}",value);
        List<WorksVO> list = worksService.getWorksByTitle(value);
        return Result.success(list);
    }


@PostMapping("/addWorks")
public Result addWorks(@RequestBody WorksDTO worksDTO){
    log.info("添加作品：{}",worksDTO);
    boolean b = worksService.AddWorks(worksDTO);
    if (!b)Result.fail();
    return Result.success();
}


    @PutMapping("/EditWorks")
    public Result EditWorks(@RequestBody WorksDTO worksDTO){
        log.info("修改作品：{}",worksDTO);
        boolean b = worksService.EditWorks(worksDTO);
        if (!b)Result.fail();
        return Result.success();
    }
    @PutMapping("/EditWorksIsShow/{worksId}/{isShow}")
    public Result EditWorksIsShow(
            @PathVariable("worksId") Long worksId,
            @PathVariable("isShow") Long isShow){
        log.info("修改{}作品显示字段：{}",worksId,isShow);
        boolean b = worksService.EditWorksIsShow(worksId,isShow);
        if (!b)Result.fail();
        return Result.success();
    }

    @DeleteMapping("/DeleteWorks/{id}")
    public Result DeleteWorks(@PathVariable("id") Long id){
        log.info("删除作品：{}",id);
        boolean b = worksService.DeleteWorks(id);
        if (!b)Result.fail();
        return Result.success();
    }


    @GetMapping("/worksByCategory/{categoryId}")
    public Result<List<WorksVO>> worksByCategory(@PathVariable("categoryId") Long categoryId){
        log.info("根据类别ID查询作品列表：{}",categoryId);
        List<WorksVO> list = worksService.worksByCategory(categoryId);
        return Result.success(list);
    }


    @GetMapping("/worksByNoCategory")
    public Result<List<WorksVO>> worksByNoCategory(){
        log.info("查询没有分类的作品列表.....");
        List<WorksVO> list = worksService.worksByNoCategory();
        return Result.success(list);
    }


    /*
     * SELECT * FROM tb_category WHERE id NOT IN (SELECT category_id FROM tb_works_category WHERE works_id = 1)
     * SELECT * FROM tb_category WHERE id IN (SELECT category_id FROM tb_works_category WHERE works_id = 1)
     * */
    @GetMapping("/getOneWorksByNoCategory/{worksId}")
    public Result<List<CategoryVO>> getWorksByNoCategory(@PathVariable("worksId") Long worksId){
        log.info("查询指定作品的未分类的分类列表.....");
        List<CategoryVO> list = worksService.getWorksByNoCategory(worksId);
        return Result.success(list);
    }
    @GetMapping("/getOneWorksByCategory/{worksId}")
    public Result<List<CategoryVO>> getWorksByCategory(@PathVariable("worksId") Long worksId){
        log.info("查询指定作品的已分类的分类列表.....");
        List<CategoryVO> list = worksService.getWorksByCategory(worksId);
        return Result.success(list);
    }


    @PostMapping("AddCategoryByWorks")
    public Result AddCategoryByWorks(@RequestBody AddCategoryByWorksDTO addCategoryByWorksDTO){
        log.info("根据传入的类别IDS：{}和当前作品的ID：{}",addCategoryByWorksDTO.getCategoryIds(),addCategoryByWorksDTO.getCurrentWorksId());
        boolean b = worksService.AddCategoryByWorks(addCategoryByWorksDTO);
        if (!b) return Result.fail();
        return Result.success();
    }

    @GetMapping("/getWorksListByShow/{isShow}")
    public Result<List<CategoryVO>> getWorksListByShow(@PathVariable("isShow") Long isShow){
        log.info("查询指定作品的未分类的分类列表.....");
        List<CategoryVO> list = worksService.getWorksByNoCategory(isShow);
        return Result.success(list);
    }

    //查询推荐显示或不推荐显示作品
    @GetMapping("/worksByIsShow/{isShow}")
    public Result<List<WorksVO>> worksByIsShow(@PathVariable("isShow") Long isShow){
        log.info("查询查询推荐显示或不推荐显示作品列表.....");
        List<WorksVO> list = worksService.getWorksByIsShow(isShow);
        return Result.success(list);
    }
}
